package com.example.qmt.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.example.qmt.advertising.AdvertisingFragment;
import com.example.qmt.advertising.AdvertisingFragmentStateAdapter;
import com.example.qmt.advertising.AdvertisingActivity;
import com.example.qmt.databinding.FragmentHomeBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding homeBinding;
    private Context context;

    private ArrayList<Fragment> fragments;
    private ArrayList<String> titles;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        homeBinding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = homeBinding.getRoot();

        context = getContext();

        homeBinding.advertising.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, AdvertisingActivity.class);
                startActivity(intent);
            }
        });

        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //标题栏
        init();

        //设置滑动方向
        homeBinding.homePager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

        AdvertisingFragmentStateAdapter advertisingAdapter = new AdvertisingFragmentStateAdapter(this,fragments);
        homeBinding.homePager.setAdapter(advertisingAdapter);


        //关联TabLayout 添加attach()
        new TabLayoutMediator(homeBinding.titleTab, homeBinding.homePager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position));
            }
        }).attach();

        homeBinding.homePager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                Log.d("amy",position+"");
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        homeBinding = null;
    }

    public void init(){

        fragments= new ArrayList<>();
        fragments.add(new AdvertisingFragment());
        fragments.add(new HomeFragment());

        titles = new ArrayList<>();
        titles.add("广告墙");
        titles.add("推荐");
    }
}